package com.nightpiter.model.objects;

import com.nightpiter.exceptions.TimeFormatException;

/**
 * Time class
 * 
 * working with Time typed values
 * 
 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
 * @author Ilya Shourigyn (honorik@gmail.com)
 * 
 */

public class Time {

	private int seconds;
    private int minutes;
    private int hours;

    /**  
     * @param hours  - Hours
     * @param minutes - Minutes
     * @param seconds - Seconds
     */
    public Time(int hours,int minutes, int seconds){
        this.seconds=seconds%60;
        this.minutes=(minutes%60+seconds/60)%60;
        this.hours=hours+seconds/3600+minutes/60;
    }

    /**
     * 
     * @param seconds - Time in seconds
     */
    public Time(int seconds){
        this.seconds=seconds%60;
        this.minutes=(seconds/60)%60;
        this.hours=hours+seconds/3600;
    }    

    /**
     * 
     * @param strTime time in format "hh::mm::ss" or "mm::ss" or "ss"
     * @throws TimeFormatException - in case of wrong time format
     */
    public Time(String strTime) throws TimeFormatException {

		String[] strTimeParts = strTime.split(":");

        switch (strTimeParts.length) {
        case 1:
            seconds = Integer.parseInt(strTimeParts[0]);
            break;
        case 2:
            minutes = Integer.parseInt(strTimeParts[0]);
            seconds = Integer.parseInt(strTimeParts[1]);
            break;
        case 3:
            hours = Integer.parseInt(strTimeParts[0]);
            minutes = Integer.parseInt(strTimeParts[1]);
            seconds = Integer.parseInt(strTimeParts[2]);
            break;
        default:
        	throw new TimeFormatException();
        }


    }    
    /**
     * Represent Time as string in format "hh:mm:ss" if Hours is more then null, 
     * and "mm:ss" in other way
     */
    public String toString() {
		String result = "";
		if (hours != 0) {
			result += hours + ":";
		}
		if (minutes < 10) {
			result += "0";
		}
		result += minutes+":";
		if (seconds < 10) {
			result += "0";
		}
		result += seconds;
        return result;
    }


    /**
     * 
     * @param strTime time in format "hh::mm::ss" or "mm::ss" or "ss"
     * @return time in seconds
     * @throws TimeFormatException - in case of wrong time format
     */
	public static int stringToInt(String strTime) throws TimeFormatException  {
		Time time = new Time(strTime);
        return time.hours*3600+time.minutes*60+time.seconds;
    }


}
